$(function() {
	// 得到焦点
	$("#password").focus(function() {
		$("#left_hand").animate( {
			left : "150",
			top : " -38"
		}, {
			step : function() {
				if (parseInt($("#left_hand").css("left")) > 140) {
					$("#left_hand").attr("class", "left_hand");
				}
			}
		}, 2000);
		$("#right_hand").animate( {
			right : "-64",
			top : "-38px"
		}, {
			step : function() {
				if (parseInt($("#right_hand").css("right")) > -70) {
					$("#right_hand").attr("class", "right_hand");
				}
			}
		}, 2000);
	});
	// 失去焦点
	$("#password").blur(function() {
		$("#left_hand").attr("class", "initial_left_hand");
		$("#left_hand").attr("style", "left:100px;top:-12px;");
		$("#right_hand").attr("class", "initial_right_hand");
		$("#right_hand").attr("style", "right:-112px;top:-12px");
	});

	if ($.cookie("rmbUser") == "true") {
		$("#ck_rmbUser").prop("checked", true);
		$("#username").val($.cookie("username"));
		$("#password").remove();
		$("#pass")
				.append("<input id='password'  type='password' class='ipt'/>");
		$("#password").val($.cookie("password"));
	}
	$("#loginButton").click(function() {
		if (check()) {
			login();
		}
	});

});

// 记住用户名密码
function save() {
	if ($("#ck_rmbUser").prop("checked")) {
		var username = $("#username").val();
		var password = $("#password").val();
		$.cookie("rmbUser", "true", {
			expires : 7
		}); // 存储一个带7天期限的cookie
		$.cookie("username", username, {
			expires : 7
		});
		$.cookie("password", password, {
			expires : 7
		});
	} else {
		$.cookie("rmbUser", "false", {
			expire : -1
		});
		$.cookie("username", "", {
			expires : -1
		});
		$.cookie("password", "", {
			expires : -1
		});
	}
};

function check() {
	var username = $("#username").val();
	var password = $("#password").val();
	var randCode = $("#randCode").val();
	if (username == "" || username == "请输入用户名") {
		$("#tip").text("请输入用户名!");
		$("#username").focus();
		return false;
	}
	if (password == "" || password == "请输入密码") {
		$("#tip").text("请输入密码!");
		$("#password").focus();
		return false;
	}
	if (randCode == "" || randCode == "请输入验证码") {
		$("#tip").text("请输入验证码!");
		$("#password").focus();
		return false;
	}
	$("#tip").text("");
	return true;
}

function login() {
	var data = {
		userName : $("#username").val(),
		password : $("#password").val(),
		randCode : $("#ranCode").val(),
		langCode : 'zh-cn'
	};
	showOverlay();
	$.doAjaxCall("loginController.do?checkuser", data,
			loginCallback, false);

}

function loginCallback(data) {
	if (data.success) {
		save();
		
		window.location.href = "loginController.do?login";
		
	} else {
		
		hideOverlay();
		$("#tip").text(data.msg);

	}
}